package com.lm.activiti.boot.template;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @date 2014-11-24
 * @author xiejindun
 */
public class BaseRestTestTemplate {

	/**
	 * post
	 * 
	 * @param url
	 * @param objectDto
	 * @throws RuntimeException
	 */
	public static void httpPost(String url, Object objectDto) throws RuntimeException {
		httpPost(url, objectDto, null);
	}

	/**
	 * post (包含header)
	 * 
	 * @param url
	 * @param objectDto
	 * @param headerMap
	 * @throws RuntimeException
	 */
	public static void httpPost(String url, Object objectDto, Map<String, Object> headerMap) throws RuntimeException {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(url);
		Builder request = target.request();
		if (headerMap != null) {
			for (Entry<String, Object> hmap : headerMap.entrySet()) {
				request.header(hmap.getKey(), hmap.getValue());
			}
		}
		Response response = request.post(Entity.entity(objectDto, MediaType.APPLICATION_JSON_TYPE));

		try {
			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed with HTTP error code : " + response.getStatus());
			}
			System.out.println("The response is " + response.readEntity(String.class));
		} finally {
			response.close();
			client.close(); // 在真正开发中不要每次关闭client，比如HTTP长连接是由client持有的
		}
	}

	/**
	 * get
	 * 
	 * @param url
	 * @param params
	 * @throws RuntimeException
	 */
	public static void httpGet(String url, Map<String, String> params) throws RuntimeException {
		httpGet(url, params, null);
	}

	/**
	 * get (包含header)
	 * 
	 * @param url
	 * @param params
	 * @param headerMap
	 * @throws RuntimeException
	 */
	public static void httpGet(String url, Map<String, String> params, Map<String, Object> headerMap)
			throws RuntimeException {

		if (params != null) {
			for (Entry<String, String> param : params.entrySet()) {
				if (url.contains("?")) {
					url = url.concat("&");
				} else {
					url = url.concat("?");
				}
				url = url.concat(param.getKey()).concat("=").concat(param.getValue());
			}
		}

		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(url);
		Builder request = target.request();
		if (headerMap != null) {
			for (Entry<String, Object> hmap : headerMap.entrySet()) {
				request.header(hmap.getKey(), hmap.getValue());
			}
		}
		Response response = request.get();

		try {
			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed with HTTP error code : " + response.getStatus());
			}
			System.out.println("The response is " + response.readEntity(String.class));
		} finally {
			response.close();
			client.close(); // 在真正开发中不要每次关闭client，比如HTTP长连接是由client持有的
		}
	}
	
	/**
	 * post (不包含header)
	 * @param url
	 * @param objectDto
	 * @throws RuntimeException
	 */
	public static void httpPostForm(String url, Map<String, String> paramMap) throws RuntimeException {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(url);
		Builder request = target.request();
		Form formData = new Form();
		for (Entry<String, String> pmap : paramMap.entrySet()) {
			formData.param(pmap.getKey(), pmap.getValue());
		}
		Response response = request.post(Entity.form(formData));
        
		try {
		    if (response.getStatus() != 200) {
		        throw new RuntimeException("Failed with HTTP error code : " + response.getStatus() );
		    }
		    System.out.println("The response is " + response.readEntity(String.class));
		} finally {
		    response.close();
		    client.close(); // 在真正开发中不要每次关闭client，比如HTTP长连接是由client持有的
		}
	}

	/**
	 * post (包含header)
	 * @param url
	 * @param objectDto
	 * @param headerMap
	 * @throws RuntimeException
	 */
	public static void httpPostForm(String url, Map<String, String> paramMap, Map<String, Object> headerMap) throws RuntimeException {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(url);
		Builder request = target.request();
		if(headerMap != null) {
			for (Entry<String, Object> hmap : headerMap.entrySet()) {
				request.header(hmap.getKey(), hmap.getValue());
			}
		}
		Form formData = new Form();
		for (Entry<String, String> pmap : paramMap.entrySet()) {
			formData.param(pmap.getKey(), pmap.getValue());
		}
		Response response = request.post(Entity.form(formData));
        
		try {
		    if (response.getStatus() != 200) {
		        throw new RuntimeException("Failed with HTTP error code : " + response.getStatus() );
		    }
		    System.out.println("The response is " + response.readEntity(String.class));
		} finally {
		    response.close();
		    client.close(); // 在真正开发中不要每次关闭client，比如HTTP长连接是由client持有的
		}
	}
	
	public static String httpPost2(String url, Map<String, String> params) throws RuntimeException {
		if(params != null) {
			for (Entry<String, String> param : params.entrySet()) {
				if(url.contains("?")) {
					url = url.concat("&");
				} else {
					url = url.concat("?");
				}
				url = url.concat(param.getKey()).concat("=").concat(param.getValue());
			}
		}
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(url);
		Builder request = target.request();
		request.header("Content-Type", "application/x-www-form-urlencoded");
		
		Response response = request.post(Entity.entity(null, MediaType.APPLICATION_JSON_TYPE));
		try {
		    if (response.getStatus() != 200) {
		        throw new RuntimeException("Failed with HTTP error code : " + response.getStatus() );
		    }
		    return response.readEntity(String.class);
		} finally {
		    response.close();
		    client.close(); // 在真正开发中不要每次关闭client，比如HTTP长连接是由client持有的
		}
	}

	
}
